package com.game.worm.mobile.view.model;

import android.util.SparseArray;
import com.dx.as.listener.IPresenter;

import java.util.Map;

/**
 * Created by d_xxiang on 2018/3/27.
 */

public class Presenter extends IPresenter{
    private SparseArray<OnDataChangedListener> sparseArray = new SparseArray<>();

    private static Presenter presenter;
    private Model model;
    private  Presenter(){
        model = new Model();
    }

    public static Presenter getInstance(){
        if (presenter == null){
            synchronized (Presenter.class){
                if (presenter == null){
                    presenter = new Presenter();
                }
            }
        }
        return presenter;
    }

    @Override
    public void onResponse(Object response, int flg) {
        if (response != null){
            sparseArray.get(flg).onSuccess(response);
            sparseArray.remove(flg);
        }
    }

    public void doHttpRequest(String key, Map<String,Object> map,OnDataChangedListener listener){
        setDataChangedListener(model.doHttp(key,map),listener);
    }

    private void setDataChangedListener(int code,OnDataChangedListener listener){
       sparseArray.put(code,listener);
    }

}
